import {bullet_list,user_plane as plane} from './user_plane.js';
import {ctx,score,boom_list,boss_bullet_list,award_list} from './canvas.js'
import { user_bullet ,user_bullet2,user_bullet3} from './bullet.js';
import {out,hitBox,xyseed,boom,pixelHitTest,imgMap} from './tool.js'
import { enemy_list } from './canvas.js';
import { show_boss,init_user_plane,crate_boss,boss} from './boss.js';
import enemy_module from './enemy.js';
import { red_wingman ,wingman_bullet,wingman_list} from './wingman.js';
window.onload=()=>{
  
    var max_width = document.body.offsetWidth;
    var max_height = document.documentElement.clientHeight;
    const user_plane=new plane()
    init_user_plane(user_plane)
    document.getElementById("canvas").width = max_width;
    document.getElementById("canvas").height = max_height
    ctx.imageSmoothingEnabled = false;
    let ani=null
    //计步器
    let step=0
    //删除列表
    let del_list = [];
    //方向键控制
    let key={
      top:false,
      left:false,
      right:false,
      bottom:false
    }


    document.addEventListener('keydown', (eve)=>{
        switch(eve.keyCode){
          case 37:
            key.left=true
            break;
          case 38:
            key.top=true
            break;
          case 39:
            key.right=true
            break;
          case 40:
            key.bottom=true
            break;
        }
        return false
    })
    document.addEventListener('keyup', (eve)=>{
      switch(eve.keyCode){
        case 37:
          key.left=false
          break;
        case 38:
          key.top=false
          break;
        case 39:
          key.right=false
          break;
        case 40:
          key.bottom=false
          break;
      }
  })
  const boss1=new boss(user_plane)
  const menu_dom=document.querySelector('.main_view')
    function draw (){
        
        ctx.clearRect(0, 0, max_width, max_height + 200);
        if(!show_boss&&score>=150){
          crate_boss()
          boss1.create()
          enemy_list.push(boss1)
        }
        
        //发射子弹
        if(step%20==0){
          switch(user_plane.atk_level){
            case 1:
              var bullet = new user_bullet(user_plane)
              bullet_list.push(bullet)
              break
            case 2:
              var bullet = new user_bullet2(user_plane)
              bullet_list.push(bullet)
              break
            case 3:
              var bullet = new user_bullet3(user_plane)
              bullet_list.push(bullet)
              break
          }
        }
        //创建敌人
        if(step%50==0 &&!show_boss){
          let class1=Math.ceil(Math.random()*2)
          // let class1=2
          switch(class1){
            case 1:
              var enemy = new enemy_module.enemy1(user_plane);
              enemy_list.push(enemy);
              enemy.active()
              break
            case 2:
              var enemy = new enemy_module.enemy2(user_plane);
              enemy_list.push(enemy);
              break
            case 3:
              var enemy = new enemy_module.enemy3(user_plane);
              enemy_list.push(enemy);
              break
          }
        }
        //便利玩家子弹
        bullet_list.forEach((item,index)=>{
            if(out(item)){
                del_list.unshift({ index: index, arr: bullet_list });
                return
            }else{
                ctx.drawImage(item.dom,item.left,item.top,item.width,item.height)
                item.move()
            }
            //检测子弹和敌机碰撞
            enemy_list.some((item1,index1)=>{
              if(pixelHitTest(imgMap(item,20),imgMap(item1,30))){
                // if(hitBox(item,item1)){
                item1.life-=item.atk
                del_list.unshift({ index: index, arr: bullet_list });
                return true
              }
            })
        })
        //遍历敌人
        enemy_list.forEach((item,index)=>{
          if(item.top>=max_height+200){
            del_list.unshift({ index: index, arr: enemy_list });
            return
          }
          if(item.life<=0){
            item.die()
            del_list.unshift({ index: index, arr: enemy_list });
            return
          }
          ctx.drawImage(item.dom,item.left,item.top,item.width,item.height)
          item.move()
          if(pixelHitTest(imgMap(item,20),imgMap(user_plane,20))){
            console.log('装上去了');
            item.die()
            user_plane.changeHP(-10)
            del_list.unshift({ index: index, arr: enemy_list });
            return
          }
          
        })
        //遍历敌人爆炸
        boom_list.forEach((item,index)=>{
          ctx.drawImage(item.animate[item.animate_index],item.left,item.top,item.width,item.height)
          if(step%5==0){
            item.animate_index++
          }
          if(item.animate_index>=5){
            del_list.unshift({ index: index, arr: boom_list });
          }
        })
        //遍历奖励
        award_list.forEach((item,index)=>{
          if(hitBox(item,user_plane)){
            switch(item.style){
              case 'wingman':
                if(!user_plane.wingman){
                  user_plane.wingman=true
                  for(let i=0;i<2;i++){
                    var wingman_item=new red_wingman()
                    wingman_item.index=i
                    wingman_list.push(wingman_item)
                  }
                  // wingman1.create()
                }
                
                break
              case 'power_up':
                if(user_plane.atk_level<3){
                  user_plane.atk_level++
                }
                
                break
            }
            del_list.unshift({ index: index, arr: award_list });
          }else{
            ctx.drawImage(item.dom,item.left,item.top,item.width,item.height)
            item.move()
          }
        })
        //便利僚机子弹
        wingman_bullet.forEach((item,index)=>{
          let del=false
          //检测子弹和敌机碰撞
          enemy_list.some((item1,index1)=>{
            if(hitBox(item,item1)){
              // if(hitBox(item,item1)){
              item1.life-=item.atk
              if(item.style!='laser'){
                del_list.unshift({ index: index, arr: wingman_bullet });
                del=true
                console.log(item.style)
                return true
              }
              
            }
          })
          if(out(item)&&!del){
            del_list.unshift({ index: index, arr: wingman_bullet });
            return
          }else{
              ctx.drawImage(item.dom,item.left,item.top,item.width,item.height)
              item.move()
          } 
      })

        wingman_list.forEach(item=>{
          item.run(user_plane)
          ctx.drawImage(item.dom,item.left,item.top,item.width,item.height)
        })
        // skill_list.forEach((item,index)=>{
        //   if(step%10==0){
        //     item.run()
        //   }
        //   if(item.distroy){
        //     del_list.unshift({ index: index, arr: skill_list });
        //     return
        //   }
        //   ctx.drawImage(item.dom,item.left,item.top,item.width,item.height)
          
        //   // if(pixelHitTest(imgMap(item,30),imgMap(user_plane,30))){
        //   //   user_plane.life-=item.atk
        //   // }
        // })
        //便利敌方子弹
        boss_bullet_list.forEach((item,index)=>{
          if(out(item)){
            del_list.unshift({ index: index, arr: boss_bullet_list });
          }
          ctx.drawImage(item.dom,item.left,item.top,item.width,item.height)
          if(pixelHitTest(imgMap(item,10),imgMap(user_plane,10))){
            
            user_plane.changeHP(-item.atk)
            del_list.unshift({ index: index, arr: boss_bullet_list });
          }
          

        })
        del_list.forEach((item) => {
            clearInterval(item?.time) 
            item.arr.splice(item.index, 1); 
          });
        if(step%5==0){
            user_plane.foot_index=Math.abs(user_plane.foot_index-1)
        }
        //绘画飞机喷火
        user_plane.foot()
        //飞机移动
        key.top&&user_plane.top>0?user_plane.top-=user_plane.speed:''
        key.left&&user_plane.left>0?user_plane.left-=user_plane.speed:''
        key.right&&user_plane.left<max_width-user_plane.width?user_plane.left+=user_plane.speed:''
        key.bottom&&user_plane.top<max_height-user_plane.height?user_plane.top+=user_plane.speed:''
        //绘画飞机
        ctx.drawImage(user_plane.dom,user_plane.left,user_plane.top,user_plane.width,user_plane.height)
        if(user_plane.unbeatable_time){
          user_plane.drawCovered()
        }
        step++
        del_list=[]
        // if(step%500==0){
        //   console.log(skill_list)
        // }
        ani=  window.requestAnimationFrame(draw);
        if(user_plane.life <=0){
          window.cancelAnimationFrame(ani)
          menu_dom.style.display="flex"
        }
        if(boss1.life<=0){
          window.cancelAnimationFrame(ani)
          menu_dom.style.display="flex"
        }
    }
    ani= window.requestAnimationFrame(draw);
}